#include "EC200T.h"
#include "stdlib.h"
#include "string.h"
#include "usart.h"
#include "main.h"
#include "gpio.h"


extern uint8_t TBOX_RSSI;
extern uint8_t Tx_Err_Cnt;

extern unsigned char LPUART1_Rx_Buf[MAX_REC_LENGTH];             
extern unsigned int  LPUART1_Rx_cnt;                 
extern unsigned char LPUART1_temp[REC_LENGTH];


extern uint8_t Dest_IP[16];
extern uint8_t Dest_PORT[6];
extern uint8_t HeartBeat_Packet[100];



void PW_ON_EC200T(void)
{
	EC200T_PW_OFF;
	EC200T_RESET_ON;
	HAL_Delay(1000);
	EC200T_PW_ON;
	HAL_Delay(500);
	EC200T_RESET_OFF;
	HAL_Delay(2000);
}
/*
void PW_ON_EC200T(void) 
{
	EC200T_PW_OFF;
	
	
	EC200T_RESET_OFF;
	EC200T_PW_ON;
	HAL_Delay(2000);
	
}
*/
/*
*********************************************************************************************************
*	������:   EC200T_Require_AT
*	����˵��: ���EC200T��ATָ���Ƿ��������ȴ�EC200T����,�����򷵻�1�����򷵻�0
*	����ֵ:   EC200T�Ƿ����1��0
*   ���ߣ�    ����
*   ���ڣ�    2020-07-17
*********************************************************************************************************/
uint8_t EC200T_Require_AT(void)
{
	uint8_t i=30;
	uint8_t Requiere_AT_cmd[4]={'A','T','\r','\n'}; 
	LPUART1_Rx_cnt = 0;
	while(i--) 
	{
		LPUART1_Rx_cnt = 0;
		MCU_SentData_ToEC200T(Requiere_AT_cmd,4);
		HAL_Delay(1500);	
        SysPrintf("len=%d\n",LPUART1_Rx_cnt);
        LPUART1_Rx_Buf[LPUART1_Rx_cnt]=0 ;
        SysPrintf("%s\r\n",LPUART1_Rx_Buf);
				
        if( strstr(LPUART1_Rx_Buf,"OK")!=NULL) 
        {
            SysPrintf("EC200T exist...\r\n");
            return 1;
        }
        else 
        {
           ;
        }
	}
	SysPrintf("not detect EC200T...\r\n");
	return 0;
}

/*
*********************************************************************************************************
*	������:   EC200T_Close_echo
*	����˵��: �ر�EC200T�Ļ���
*	����ֵ:   ��
*   ���ߣ�    ����
*   ���ڣ�   2020-07-17
*********************************************************************************************************/
void EC200T_Close_echo(void)
{
	uint8_t Close_echo_cmd[6]={'A','E','T','0','\r','\n'};
	SysPrintf("EC200T close echo begin\r\n");
	MCU_SentData_ToEC200T(Close_echo_cmd,6);
	HAL_Delay(500);	
}

/*
*********************************************************************************************************
*	������:   EC200T_Get_CSQ
*	����˵��: ��ȡEC200T���ź�ǿ��
*	����ֵ:   ��
*   ���ߣ�    ����
*   ���ڣ�    2020-07-17
*********************************************************************************************************/
void EC200T_Get_CSQ(void)
{
	uint8_t i;
	uint8_t Get_CSQ_cmd[8]={'A','T','+','C','S','Q','\r','\n'}; 
	LPUART1_Rx_cnt = 0;
	SysPrintf("EC200T get CSQ begin\r\n");
	MCU_SentData_ToEC200T(Get_CSQ_cmd,8);
	HAL_Delay(500);
	SysPrintf("len=%d\n",LPUART1_Rx_cnt);
    LPUART1_Rx_Buf[LPUART1_Rx_cnt]=0 ;
    SysPrintf("%s\r\n",LPUART1_Rx_Buf);

	if(LPUART1_Rx_cnt>10)
	{
		for(i=0;i<LPUART1_Rx_cnt-3;i++)
		{
			if( (LPUART1_Rx_Buf[i]==':') && (LPUART1_Rx_Buf[i+1]==' ') )
			{
				TBOX_RSSI=((LPUART1_Rx_Buf[i+2]-0x30)<<3)+((LPUART1_Rx_Buf[i+2]-0x30)<<1)+(LPUART1_Rx_Buf[i+3]-0x30);
				if((TBOX_RSSI>=0) && (TBOX_RSSI<=31)) TBOX_RSSI=(TBOX_RSSI<<1)-113;
					else if((TBOX_RSSI>=100) && (TBOX_RSSI<=190)) TBOX_RSSI=TBOX_RSSI-216;
						else TBOX_RSSI=-120;
				SysPrintf("4G signal is:%02X",TBOX_RSSI);
				SysPrintf("\r\n");
				break;
			}
		}
	}
}

/*
*********************************************************************************************************
*	������:   EC200T_Get_Version
*	����˵��: ��ȡEC200T�İ汾��
*	����ֵ:   ��
*   ���ߣ�    ����
*   ���ڣ�    2020-07-17
*********************************************************************************************************/
void EC200T_Get_Version(void)
{
	uint8_t Get_version_cmd[5]={'A','T','I','\r','\n'}; 
	SysPrintf("EC200T Check version number begin\r\n");
	LPUART1_Rx_cnt = 0;
	MCU_SentData_ToEC200T(Get_version_cmd,5);
	HAL_Delay(500);
	SysPrintf("len=%d\n",LPUART1_Rx_cnt);
    LPUART1_Rx_Buf[LPUART1_Rx_cnt]=0 ;
    SysPrintf("%s\r\n",LPUART1_Rx_Buf);

}

/*
*********************************************************************************************************
*	������:   EC200T_Check_SIMCard
*	����˵��: ���SIM���Ƿ���λ
*	����ֵ:   SIM���ڷ���1�����򷵻�0
*   ���ߣ�    ����
*   ���ڣ�    2020-07-17
*********************************************************************************************************/
uint8_t EC200T_Check_SIMCard(void)
{
	uint8_t j=7;
	uint8_t Check_SIMCard_cmd[10]={'A','T','+','C','P','I','N','?','\r','\n'}; 
	LPUART1_Rx_cnt = 0;
	SysPrintf("EC200T Check SIM Card begin\r\n");
	while(j--)
	{	
		LPUART1_Rx_cnt = 0;
		MCU_SentData_ToEC200T(Check_SIMCard_cmd,10);
		HAL_Delay(1000);
		SysPrintf("len=%d\n",LPUART1_Rx_cnt);
		LPUART1_Rx_Buf[LPUART1_Rx_cnt]=0 ;
		SysPrintf("%s\r\n",LPUART1_Rx_Buf);
		if(strstr(LPUART1_Rx_Buf,"READY")!=NULL)
        {
            SysPrintf("SIM-Card exist...\r\n");
            return 1;
        }
        else 
        {
           ;
        }
	}
	SysPrintf("SIM-Card not exist...\r\n");
	return 0;
}


uint8_t EC200T_Request_IMEI(void)
{
	uint8_t EC200T_CGSN_cmd[9+1]={'A','T','+','C','G','S','N','\r','\n'};  
  uint8_t i=5;
	uint8_t j=0;
	uint8_t k=0;
  SysPrintf("Request IMEI...\r\n");
  while(i--)
  {
		LPUART1_Rx_cnt = 0;
    MCU_SentData_ToEC200T(EC200T_CGSN_cmd,9);
    HAL_Delay(1000);
    SysPrintf("len=%d\n",LPUART1_Rx_cnt);
    LPUART1_Rx_Buf[LPUART1_Rx_cnt] = 0;
		SysPrintf("%s\n",LPUART1_Rx_Buf);

    if(strstr(LPUART1_Rx_Buf,"OK")!=NULL)
    {
			if(LPUART1_Rx_cnt>=18)
			{
				for(j=0;j<LPUART1_Rx_cnt-15;j++)
					if( (LPUART1_Rx_Buf[j]=='8') && (LPUART1_Rx_Buf[j+1]=='6') )
					{
						for(k=0;k<15;k++)
						{
							
						}
					}
			}
			return 1 ;
		}
		else 
		{
			SysPrintf("read IMEI error\r\n");
		}       
   }
    SysPrintf("read IMEI error\n");
    return 0 ;
}

void EC200T_Get_ICCID(void)
{
	uint16_t i;
	uint8_t j;
	uint8_t k=5;
	uint8_t Get_ICCID_cmd[10]={'A','T','+','Q','C','C','I','D','\r','\n'}; 
	LPUART1_Rx_cnt = 0;
	SysPrintf("Get EC200T ICCID begin\r\n");
	LPUART1_Rx_cnt = 0;
	while(k--)
	{
		LPUART1_Rx_cnt = 0;
		MCU_SentData_ToEC200T(Get_ICCID_cmd,10);
		HAL_Delay(1000);
		SysPrintf("len=%d\n",LPUART1_Rx_cnt);
		LPUART1_Rx_Buf[LPUART1_Rx_cnt]=0 ;
		SysPrintf("%s\r\n",LPUART1_Rx_Buf);

		if( (LPUART1_Rx_cnt>=30) && (strstr(LPUART1_Rx_Buf,"OK")!=NULL))
		{
			for(i=0;i<LPUART1_Rx_cnt-20;i++)
			{
				if((LPUART1_Rx_Buf[i]=='Q') && (LPUART1_Rx_Buf[i+1]=='C') &&(LPUART1_Rx_Buf[i+2]=='C') &&(LPUART1_Rx_Buf[i+3]=='I') &&(LPUART1_Rx_Buf[i+4]=='D') &&(LPUART1_Rx_Buf[i+5]==':') &&(LPUART1_Rx_Buf[i+6]==' '))
				{
					for(j=0;j<20;j++)
						HeartBeat_Packet[33+j] = LPUART1_Rx_Buf[i+7+j];
					
					return ;
				}
			}
		}
	}
}
/*
*********************************************************************************************************
*	������:   EC200T_Check_Register_GSM
*	����˵��: �鿴�Ƿ�ע��GSM����
*	����ֵ:   ע����GSM���緵��1�����򷵻�0
*   ���ߣ�    ����
*   ���ڣ�    2020-07-17
*********************************************************************************************************/
uint8_t EC200T_Check_Register_GSM(void)
{
	uint8_t j=7;
	uint8_t Check_Register_GSM_cmd[10]={'A','T','+','C','R','E','G','?','\r','\n'}; 
	LPUART1_Rx_cnt = 0;
	SysPrintf("EC200T Check Register GSM begin\r\n");
	while(j--)
	{	
		LPUART1_Rx_cnt = 0;
		MCU_SentData_ToEC200T(Check_Register_GSM_cmd,10);
		HAL_Delay(1500);	
		SysPrintf("len=%d\n",LPUART1_Rx_cnt);
		LPUART1_Rx_Buf[LPUART1_Rx_cnt]=0 ;
		SysPrintf("%s\r\n",LPUART1_Rx_Buf);

		if( (strstr(LPUART1_Rx_Buf,"+CREG: 0,1")!=NULL) || (strstr(LPUART1_Rx_Buf,"+CREG: 0,5")!=NULL) )
		{
			SysPrintf("4G Register GSM OK...\r\n");
            return 1;
		}
		else
		{
			;
		}
	}
	SysPrintf("4G Register GSM error...\r\n");
    return 0;
}

/*
*********************************************************************************************************
*	������:   EC200T_Check_Register_GPRS
*	����˵��: �鿴�Ƿ�ע��GPRS����
*	����ֵ:   ע����GPRS���緵��1�����򷵻�0
*   ���ߣ�    ����
*   ���ڣ�    2020-07-17
*********************************************************************************************************/
uint8_t EC200T_Check_Register_GPRS(void)
{
	uint8_t j=8;
	uint8_t Check_Register_GPRS_cmd[11]={'A','T','+','C','G','R','E','G','?','\r','\n'}; 
	LPUART1_Rx_cnt = 0;
	SysPrintf("EC200T Check Register GPRS begin\r\n");
	while(j--)
	{	
		LPUART1_Rx_cnt = 0;
		MCU_SentData_ToEC200T(Check_Register_GPRS_cmd,11);
		HAL_Delay(2500);
		SysPrintf("len=%d\n",LPUART1_Rx_cnt);
		LPUART1_Rx_Buf[LPUART1_Rx_cnt]=0 ;
		SysPrintf("%s\r\n",LPUART1_Rx_Buf);

		if( (strstr(LPUART1_Rx_Buf,"+CGREG: 0,1")!=NULL) || (strstr(LPUART1_Rx_Buf,"+CREG: 0,5")!=NULL) || (strstr(LPUART1_Rx_Buf,"+CREG: 0,1")!=NULL))
		{
			SysPrintf("4G Register GPRS OK...\r\n");
            return 1;
		}
		else
		{
			;
		}
	}
	SysPrintf("4G Register GPRS error...\r\n");
    return 0;
}

/*
*********************************************************************************************************
*	������:   EC200T_Get_Operator
*	����˵��: ��ȡEC200T����Ӫ������
*	����ֵ:   ��
*   ���ߣ�    ����
*   ���ڣ�    2020-07-17
*********************************************************************************************************/
void EC200T_Get_Operator(void)
{
	uint8_t Get_version_cmd[10]={'A','T','+','C','O','P','S','?','\r','\n'}; 
	LPUART1_Rx_cnt = 0;
	SysPrintf("EC200T get operator type begin\r\n");
	MCU_SentData_ToEC200T(Get_version_cmd,10);
	HAL_Delay(500);
	SysPrintf("len=%d\n",LPUART1_Rx_cnt);
	LPUART1_Rx_Buf[LPUART1_Rx_cnt]=0 ;
	SysPrintf("%s\r\n",LPUART1_Rx_Buf);

}


/*
*********************************************************************************************************
*	������:   EC200T_Close_Socket
*	����˵��: �ر�EC200T��socket����
*	����ֵ:   ��
*   ���ߣ�    ����
*   ���ڣ�    2020-07-17
*********************************************************************************************************/
void EC200T_Close_Socket(void)
{
	uint8_t Close_Socket_cmd[14]={'A','T','+','Q','I','C','L','O','S','E','=','0','\r','\n'}; 
	LPUART1_Rx_cnt = 0;
	SysPrintf("EC200T close socket begin\r\n");
	MCU_SentData_ToEC200T(Close_Socket_cmd,14);
	HAL_Delay(500);	
	SysPrintf("len=%d\n",LPUART1_Rx_cnt);
	LPUART1_Rx_Buf[LPUART1_Rx_cnt]=0 ;
	SysPrintf("%s\r\n",LPUART1_Rx_Buf);
}



/*
*********************************************************************************************************
*	������:   EC200T_Connect_APN
*	����˵��: ����APN
*	����ֵ:   �������뷵��1�����򷵻�0
*   ���ߣ�    ����
*   ���ڣ�    2020-07-17
*********************************************************************************************************/
uint8_t EC200T_Connect_APN(void)
{
	uint8_t j=10;
	uint8_t Connect_APN_cmd[]="AT+QICSGP=1,1,\042CMNET\042,\042\042,\042\042,0\r\n"; 
	
	LPUART1_Rx_cnt = 0;
	
	SysPrintf("EC200T connect APN begin\r\n");
	while(j--)
	{	
		MCU_SentData_ToEC200T(Connect_APN_cmd,49);
		HAL_Delay(1000);	
		SysPrintf("len=%d\n",LPUART1_Rx_cnt);
		LPUART1_Rx_Buf[LPUART1_Rx_cnt]=0 ;
		SysPrintf("%s\r\n",LPUART1_Rx_Buf);

		if(strstr(LPUART1_Rx_Buf,"OK")!=NULL)
		{
			SysPrintf("4G connect APN OK...\r\n");
            return 1;
		}
		else
		{
			;
		}
	}
	SysPrintf("4G connect APN error...\r\n");
    return 0;
}

/*
*********************************************************************************************************
*	������:   EC200T_QIDEACT
*	����˵��: ����DEACT
*	����ֵ:   ���������1�����򷵻�0
*   ���ߣ�    ����
*   ���ڣ�    2020-07-17
*********************************************************************************************************/
uint8_t EC200T_QIDEACT(void)
{
	uint8_t j=10;
	uint8_t QIDEACT_cmd[]="AT+QIDEACT=1\r\n";
	LPUART1_Rx_cnt = 0;
	SysPrintf("EC200T QIDEACT begin\r\n");
	while(j--)
	{	
		MCU_SentData_ToEC200T(QIDEACT_cmd,14);
		HAL_Delay(500);	
		SysPrintf("len=%d\n",LPUART1_Rx_cnt);
		LPUART1_Rx_Buf[LPUART1_Rx_cnt]=0 ;
		SysPrintf("%s\r\n",LPUART1_Rx_Buf);

		if(strstr(LPUART1_Rx_Buf,"OK")!=NULL)
		{
			SysPrintf("4G Acitve QIDEACT OK...\r\n");
            return 1;
		}
		else
		{
			;
		}
	}
	SysPrintf("4G Acitve QIDEACT error...\r\n");
    return 0;
}

/*
*********************************************************************************************************
*	������:   EC200T_QIACT
*	����˵��: ����ACT
*	����ֵ:   ���������1�����򷵻�0
*   ���ߣ�    ����
*   ���ڣ�    2020-07-17
*********************************************************************************************************/
uint8_t EC200T_QIACT(void)
{
	uint8_t j=10;
	uint8_t QIACT_cmd[]="AT+QIACT=1\r\n";
	LPUART1_Rx_cnt = 0;
	SysPrintf("EC200T QIACT begin\r\n");
	while(j--)
	{	
		MCU_SentData_ToEC200T(QIACT_cmd,12);
		HAL_Delay(500);	
		SysPrintf("len=%d\n",LPUART1_Rx_cnt);
		LPUART1_Rx_Buf[LPUART1_Rx_cnt]=0 ;
		SysPrintf("%s\r\n",LPUART1_Rx_Buf);

		if(strstr(LPUART1_Rx_Buf,"OK")!=NULL)
		{
			SysPrintf("4G Acitve QIACT OK...\r\n");
            return 1;
		}
		else
		{
			;
		}
	}
	SysPrintf("4G Acitve QIACT error...\r\n");
    return 0;
}


/*
*********************************************************************************************************
*	������:   EC200T_Get_IP
*	����˵��: ��ȡIP��ַ
*	����ֵ:   ��
*   ���ߣ�    ����
*   ���ڣ�    2020-07-17
*********************************************************************************************************/
void EC200T_Get_IP(void)
{

	
	uint8_t Get_IP_cmd[]="AT+QIACT?\r\n";
	LPUART1_Rx_cnt = 0;
	SysPrintf("EC200T get ip address begin\r\n");
	MCU_SentData_ToEC200T(Get_IP_cmd,11);
	HAL_Delay(500);
	SysPrintf("len=%d\n",LPUART1_Rx_cnt);
	LPUART1_Rx_Buf[LPUART1_Rx_cnt]=0 ;
	SysPrintf("%s\r\n",LPUART1_Rx_Buf);

}

/*
*********************************************************************************************************
*	������:   EC200T_Set_IP_Port
*	����˵��: ����Ŀ��IP��ַ��˿�
*	����ֵ:   �ɹ�����1�����򷵻�0
*   ���ߣ�    ����
*   ���ڣ�    2020-07-17
*********************************************************************************************************/
uint8_t EC200T_Set_IP_Port(void)
{
	uint8_t j=10;
	
	
	uint8_t Set_IP_Port_cmd[64]={0};
	LPUART1_Rx_cnt = 0;
	sprintf(Set_IP_Port_cmd,"AT+QIOPEN=1,0,\042TCP\042,\042%s\042,%s,0,2\r\n",Dest_IP,Dest_PORT);
	Set_IP_Port_cmd[strlen(Set_IP_Port_cmd)]=0;
	SysPrintf("set EC200T ip&port cmd is: %s\r\n",Set_IP_Port_cmd);

	SysPrintf("EC200T set server ip and port begin\r\n");
	MCU_SentData_ToEC200T(Set_IP_Port_cmd,strlen(Set_IP_Port_cmd));
	while(j--)
	{	
		HAL_Delay(2500);	
		SysPrintf("len=%d\n",LPUART1_Rx_cnt);
		LPUART1_Rx_Buf[LPUART1_Rx_cnt]=0 ;
		SysPrintf("%s\r\n",LPUART1_Rx_Buf);
		if(strstr(LPUART1_Rx_Buf,"CONNECT")!=NULL)
		{
			SysPrintf("4G set ip and port OK...\r\n");
			LEDNET_ON;
            return 1;
		}
		else
		{
			;
		}
	}
	SysPrintf("4G set ip and port error...\r\n");
	LEDNET_OFF;
    return 0;
}

/*
*********************************************************************************************************
*	������:   EC200T_Set_UDP_IP_Port
*	����˵��: ����Ŀ��IP��ַ��˿�
*	����ֵ:   �ɹ�����1�����򷵻�0
*   ���ߣ�    ����
*   ���ڣ�    2020-07-17
*********************************************************************************************************/
uint8_t EC200T_Set_UDP_IP_Port(void)
{
	uint8_t j=10;
	
	
	uint8_t Set_IP_Port_cmd[64]={0};
	LPUART1_Rx_cnt = 0;
	sprintf(Set_IP_Port_cmd,"AT+QIOPEN=1,1,\042UDP\042,\042%s\042,%s,0,2\r\n",Dest_IP,Dest_PORT);  
	
	Set_IP_Port_cmd[strlen(Set_IP_Port_cmd)]=0;
	SysPrintf("set EC200T ip&port cmd is: %s\r\n",Set_IP_Port_cmd);

	SysPrintf("EC200T set server ip and port begin\r\n");
	MCU_SentData_ToEC200T(Set_IP_Port_cmd,strlen(Set_IP_Port_cmd));
	while(j--)
	{	
		LPUART1_Rx_cnt = 0;
		HAL_Delay(2500);	
		SysPrintf("len=%d\n",LPUART1_Rx_cnt);
		LPUART1_Rx_Buf[LPUART1_Rx_cnt]=0 ;
		SysPrintf("%s\r\n",LPUART1_Rx_Buf);

		if(strstr(LPUART1_Rx_Buf,"CONNECT")!=NULL)
		{
			SysPrintf("4G set ip and port OK...\r\n");
            return 1;
		}
		else
		{
			;
		}
	}
	SysPrintf("4G set ip and port error...\r\n");
    return 0;
}
		
/*
*********************************************************************************************************
*	������:   EC200T_Sent_Data   void UART2_Send_Data(unsigned char *data,unsigned int len)
*	����˵��: ��ȡIP��ַ
*	����ֵ:   ��
*   ���ߣ�    ����
*   ���ڣ�    2020-07-17
*********************************************************************************************************/
void EC200T_Sent_Data(uint8_t *pData, uint16_t Size)
{
	
	SysPrintf("EC200T sent data begin");
	MCU_SentData_ToEC200T(pData,Size);
	HAL_Delay(500);	
	
	
	
	if( (strstr(LPUART1_Rx_Buf,"NO CARRIER")!=NULL) || (strstr(LPUART1_Rx_Buf,"ERROR")!=NULL) )
	{
		Tx_Err_Cnt++;
		LPUART1_Rx_cnt = 0;
	}
	else if((strstr(LPUART1_Rx_Buf,"OK")!=NULL))
	{
		Tx_Err_Cnt = 0;
	}

	SysPrintf("Tx_Er=%d\r\n",Tx_Err_Cnt);
	
}


void MCU_SentData_ToEC200T(uint8_t *buf,uint16_t len)
{
	HAL_UART_Transmit(&hlpuart1,buf,len,1000);
	
}



uint8_t  EC200T_Init(void)
{

	
	
	SysPrintf("EC200T initial begin\r\n");
	if(EC200T_Require_AT()==0) return 0;
	EC200T_Close_echo();
	EC200T_Get_CSQ();
	EC200T_Get_Version();
	if(EC200T_Check_SIMCard()==0) return 0;
	EC200T_Request_IMEI();
	EC200T_Get_ICCID();
	
	
	if(EC200T_Check_Register_GSM()==0) return 2;
	if(EC200T_Check_Register_GPRS()==0) return 2;
	
	




	
	
	
	EC200T_Get_Operator();
	EC200T_Get_CSQ();
	EC200T_Close_Socket();
	EC200T_Connect_APN();
	EC200T_QIDEACT();
	EC200T_QIACT();
	EC200T_Get_IP();
	EC200T_Get_CSQ();
	EC200T_Set_IP_Port();
	
	
	return 1;
}		 


/* void EC200TSend_StrData(char *bufferdata)
{
    u8 untildata;
    SysPrintf("AT+QISEND=0\r\n");
    delay_ms(100);
    SysPrintf(bufferdata);
    delay_ms(100);	
    USART_SendData(USART2, (u8) 0x1a);
  while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET)
	{
	}
	delay_ms(100);
    strx=strstr((char*)RxBuffer,(char*)"SEND OK");
  while(strx==NULL)
    {
        strx=strstr((char*)RxBuffer,(char*)"SEND OK");
        delay_ms(10);
    }
    delay_ms(100);
    Clear_Buffer();
    SysPrintf("AT+QISEND=0,0\r\n");
    delay_ms(200);
    strx=strstr((char*)RxBuffer,(char*)"+QISEND:");
  while(untildata)
    {
        SysPrintf("AT+QISEND=0,0\r\n");
        delay_ms(200);
        strx=strstr((char*)RxBuffer,(char*)"+QISEND:");
        strx=strstr((char*)strx,(char*)",");
        strx=strstr((char*)(strx+1),(char*)",");
        untildata=*(strx+1)-0x30;
        Clear_Buffer();
    }

} */



/* void EC200TSend_HexData(char *bufferdata)
{
    u8 untildata;
    SysPrintf("AT+QISENDEX=0,\042%s\042\r\n",bufferdata);
    delay_ms(100);
    USART_SendData(USART2, (u8) 0x1a);
  while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET)
    {
    }
	delay_ms(100);
    strx=strstr((char*)RxBuffer,(char*)"OK");
  while(strx==NULL)
    {
            strx=strstr((char*)RxBuffer,(char*)"OK");
            delay_ms(10);
    }
    delay_ms(100);
    Clear_Buffer();
    SysPrintf("AT+QISEND=0,0\r\n");
    delay_ms(200);
    strx=strstr((char*)RxBuffer,(char*)"+QISEND:");
  while(untildata)
    {
        SysPrintf("AT+QISEND=0,0\r\n");
        delay_ms(200);
        strx=strstr((char*)RxBuffer,(char*)"+QISEND:");
        strx=strstr((char*)strx,(char*)",");
        strx=strstr((char*)(strx+1),(char*)",");
        untildata=*(strx+1)-0x30;
        Clear_Buffer();
    }


} */



/* void EC200TSend_RecAccessMode(void)
{
	
    strx=strstr((const char*)RxBuffer,(const char*)"Rec data");
    if(strx)
    {
        SysPrintf("Rec data is ok\r\n");
        delay_ms(500);
        Clear_Buffer();
    }
    strx=strstr((const char*)RxBuffer,(const char*)"NO CARRIER");
    if(strx)
    {
        while(1)
        {
                   
        }
    }


} */
/*
*********************************************************************************************************
*	�� �� ��: EC200T_Recv_Data
*	����˵��: ͸��ģʽ�½������ݣ�RecvDataOK���ʾ����ͷ�����ϴ��ɹ���RecvDataER���ʾ����ʧ�ܻ������ݴ���
*	�� �� ֵ: ��ȷ����ֵ1�����󷵻�ֵ2,û�����ݷ���2
*   ��    �ߣ�����
*   ��    �ڣ�2019-02-21
*********************************************************************************************************/ 
/* u8 EC200T_Recv_Data(void)
{
   if(Rxcouter>9)
	{
		if( (RxBuffer[0]=='R') && (RxBuffer[1]=='e') && (RxBuffer[2]=='c') && (RxBuffer[3]=='v') &&(RxBuffer[4]=='D') && (RxBuffer[5]=='a') && (RxBuffer[6]=='t') && (RxBuffer[7]=='a') && (RxBuffer[8]=='O') && (RxBuffer[9]=='K') )
		{
			Clear_Buffer();
			return 1;
		}

		else
		{
			Clear_Buffer();
			return 2;
		}
	}
	else
	{
		delay_ms(500);
		return 2;
	}
}
 */

void EC200T_ReLink(void)
{
		EC200T_Close_Socket();
		EC200T_Connect_APN();
		EC200T_QIDEACT();
		EC200T_QIACT();
		EC200T_Get_IP();
		EC200T_Set_IP_Port();
}